package passenger

import (
	v1 "gitee.com/sweetsunnyflower/carpooling-server-go/app/http/controllers/api/v1"
	"gitee.com/sweetsunnyflower/carpooling-server-go/app/models/blacklist"
	"gitee.com/sweetsunnyflower/carpooling-server-go/app/models/user"
	"gitee.com/sweetsunnyflower/carpooling-server-go/app/policies"
	"gitee.com/sweetsunnyflower/carpooling-server-go/app/requests"
	"github.com/gofiber/fiber/v2"
	"github.com/spf13/cast"
)

type BlacklistsController struct {
	v1.BaseController
}

func (controller *BlacklistsController) Index(c *fiber.Ctx) error {
	blacklists := blacklist.GetBlacklistByUserIdForPassenger(controller.Auth(c))
	return controller.Json(c, blacklists)
}

func (controller *BlacklistsController) Show(c *fiber.Ctx) error {
	blacklistModel := blacklist.Get(cast.ToUint64(c.Params("id")))
	if blacklistModel.ID == 0 {
		return controller.NotFound(c)
	}

	userId := controller.Auth(c)

	if ok := policies.CanViewBlacklist(userId, blacklistModel); !ok {
		return controller.Forbidden(c)
	}

	return controller.Json(c, blacklistModel)
}

func (controller *BlacklistsController) Store(c *fiber.Ctx) error {

	request := new(requests.BlacklistRequest)

	if errors := requests.ValidateBlacklist(c, request); len(errors) > 0 {
		return controller.ValidationError(c, errors)
	}
	blackUser := user.Get(request.BlackId)

	userId := controller.Auth(c)
	// 校验black id是否存在
	if blackUser.ID <= 0 {
		return controller.Faield(c, "用户不存在")
	}

	if blackUser.ID == userId {
		return controller.Faield(c, "不能拉黑自己")
	}

	// 此用户是否已经拉黑
	if blacklist.GetBlackItemByBlackId(userId, request.BlackId).ID > 0 {
		return controller.Faield(c, "已经拉黑此用户")
	}

	blacklistModel := blacklist.Blacklist{
		UserId:  userId,
		BlackId: request.BlackId,
		Reason:  request.Reason,
	}

	blacklistModel.Create()
	if blacklistModel.ID > 0 {
		return controller.Success(c)
	} else {
		return controller.Faield(c, "创建失败")
	}
}

func (controller *BlacklistsController) Update(c *fiber.Ctx) error {

	blacklistModel := blacklist.Get(cast.ToUint64(c.Params("id")))
	if blacklistModel.ID == 0 {
		return controller.NotFound(c)
	}

	userId := controller.Auth(c)
	if ok := policies.CanModifyBlacklist(userId, blacklistModel); !ok {
		return controller.Forbidden(c)
	}

	request := new(requests.BlacklistRequest)

	if errors := requests.ValidateBlacklist(c, request); len(errors) > 0 {
		return controller.ValidationError(c, errors)
	}

	// blacklistModel.FieldName = request.FieldName
	rowsAffected := blacklistModel.Save()
	if rowsAffected > 0 {
		return controller.Success(c)
	} else {
		return controller.Faield(c, "更新失败")
	}
}

func (controller *BlacklistsController) Delete(c *fiber.Ctx) error {

	blacklistModel := blacklist.Get(cast.ToUint64(c.Params("id")))

	if blacklistModel.ID == 0 {
		return controller.NotFound(c)
	}

	userId := controller.Auth(c)

	if ok := policies.CanDeleteBlacklist(userId, blacklistModel); !ok {
		return controller.Forbidden(c)
	}

	rowsAffected := blacklistModel.Delete()
	if rowsAffected > 0 {
		return controller.Success(c)
	}

	return controller.Faield(c, "删除失败")
}
